package com.it.controller;

import com.it.domain.Teacher;
import com.it.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    // 添加教师
    public void addTeacher() {
        System.out.println("请输入教师编号：");
        String teacherId = sc.next();
        // 判断是否存在
        if (teacherService.isExist(teacherId)) {
            System.out.println("该教师已存在！");
        } else {
            Teacher teacher = getTeacher(teacherId);
            // 调用service层
            if (teacherService.addTeacher(teacher)) {
                System.out.println("添加成功！");
            } else {
                System.out.println("添加失败！");
            }
        }
    }

    // 获取教师信息
    private Teacher getTeacher(String teacherId) {
        System.out.print("请输入姓名：");
        String name = sc.next();
        System.out.print("请输入性别：");
        String gender = sc.next();
        System.out.print("请输入年龄：");
        String age = sc.next();
        System.out.print("请输入科目：");
        String subject = sc.next();
        return new Teacher(teacherId, name, gender, age, subject);
    }

    // 删除教师
    public void deleteTeacher() {
        System.out.println("请输入要删除的教师编号：");
        String id = sc.next();
        if (teacherService.deleteTeacher(id)) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败！");
        }
    }

    // 列出所有教师
    public void listTeacher() {
        System.out.println("编号\t姓名\t性别\t年龄\t科目");
        for (Teacher teacher : teacherService.findAll()) {
            System.out.println(teacher.getTeacherId() + "\t" + teacher.getName() + "\t" + teacher.getGender() + "\t" + teacher.getAge() + "\t" + teacher.getSubject());
        }
    }

    // 修改教师
    public void updateTeacher() {
        System.out.println("请输入要修改的教师编号：");
        String id = sc.next();
        if (teacherService.isExist(id)) {
            Teacher teacher = getTeacher(id);
            if (teacherService.updateTeacher(teacher)) {
                System.out.println("修改成功！");
            } else {
                System.out.println("修改失败！");
            }
        }
    }

}
